<?php

namespace App\Services\MiniprogramToGame\HotSearch;

use App\Services\ServiceBase;
use App\Structs\HotSearchStruct;
use Illuminate\Support\Facades\Http;

class Toutiao extends ServiceBase
{
    public function reptile()
    {
        $url = 'https://www.toutiao.com/hot-event/hot-board/?origin=toutiao_pc';
        $response = Http::get($url);
        if (!$response->ok()) {
            return false;
        }

        $data = json_decode($response->body(), true);

        $list = [];
        foreach ($data['data'] as $index => $item) {
            $class           = new HotSearchStruct;
            $class->title    = $item['Title'];
            $class->hotIndex = $index+1;
            $class->hotValue = $item['HotValue'];
            $class->url      = $item['Url'];
            $class->img      = $item['Image']['url'] ?? '';
            $list[] = $class;
        }

        // 置顶
        $top_data        = $data['fixed_top_data'][0];
        $class           = new HotSearchStruct;
        $class->title    = $top_data['Title'];
        $class->isTop    = 1;
        $class->url      = $top_data['Url'];
        $list[]          = $class;

        return $list;
    }
}
